package com.gxa.modules.service.zxy.impl;

import com.gxa.common.constant.Constants;
import com.gxa.common.vo.R2;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.zxy.UserMapper;
import com.gxa.modules.pojo.Audit;
import com.gxa.modules.pojo.User;
import com.gxa.modules.service.zxy.UserService;
import com.gxa.modules.utils.DateUtils;
import com.gxa.modules.utils.MD5Utils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Author: zxy
 * Date: 2021/12/28 9:45
 * Description:
 * Version: V1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 新增用户
     * @param map
     * @return
     */
    @Override
    public R2 addUser(Map map) {
        //1、密码加密-- E10FDCg3949BA59ABBE56Ex057A20F883aE
        //2、加盐值--可以是随机数--也可以是固定一个规则gxa
        //3、散列次数--2
        SimpleHash simpleHash = new SimpleHash(MD5Utils.MD5_KEY,map.get("password"),"GXA",2);
        String newpwd = simpleHash.toString();
        //获取当前时间
        String currentTime = DateUtils.getCurrentTime();
        map.put("newpwd",newpwd);
        map.put("salt","GXA");
        map.put("user_date",currentTime);

        R2 r = new R2();
        int result = userMapper.addUser(map);
        if(result>0){
            r.setCode(Constants.SUCCESS);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("系统错误，添加失败");
        }

        return r;
    }

    /**
     * 查询用户列表
     * @param map
     * @return
     */
    @Override
    public R2 queryUserList(Map map) {
        Set<String> set = map.keySet();
        int a = 0;
        for (String key:set) {
            if(key.equals("endDate")){
                a=1;
                break;
            }
        }
        Integer currentPage = 0;
        Integer limit = 5;
        for(String key : set){
            if(key.equals("limit")){
                currentPage = (Integer)map.get("currentPage");
                limit = (Integer)map.get("limit");
                currentPage = (currentPage-1)*limit;
            }
        }
        map.put("currentPage",currentPage);
        map.put("limit",limit);
        if(a==1){
            String beginDate = (String)map.get("beginDate");
            String endDate = (String)map.get("endDate");
            beginDate = beginDate +" 00:00:00";
            endDate = endDate+" 00:00:00";
            map.put("beginDate",beginDate);
            map.put("endDate",endDate);
        }
        List<User> list = userMapper.queryUserList(map);
        System.out.println("****************************************************");
        System.out.println(list);
        System.out.println("****************************************************");
        //查询用户总共多少条
        Integer total = userMapper.queryTotal(map);
        System.out.println(total);
        R2 r = new R2();
        if(list!=null){
            for (User user:list) {
                Long userId = user.getUserId();
                Integer saleShop = userMapper.querySaleShop(userId);
                Integer tryShop = userMapper.queryTryShop(userId);
                user.setSaleShop(saleShop);
                user.setTryShop(tryShop);
            }
            r.setCode(Constants.SUCCESS);
            r.setData(list);
            r.setMsg("查询成功");
            r.setData2(total);
        }else{
            r.setMsg("查询失败");
            r.setCode(500);
        }
        return r;
    }


    /**
     * 修改用户
     * @param map
     * @return
     */
    @Override
    public R2 changeUser(Map map) {
        SimpleHash simpleHash = new SimpleHash(MD5Utils.MD5_KEY,map.get("password"),"GXA",2);
        String newpwd = simpleHash.toString();
        map.put("password",newpwd);
        int result = userMapper.changeUser(map);
        R2 r= new R2();
        if(result>0){
            r.setCode(Constants.SUCCESS);
            r.setMsg("修改成功");
        }else {
            r.setCode(500);
            r.setMsg("失败，请重试!");
        }
        return r;
    }

    /**
     * 修改用户状态
     * @param status
     * @param id
     * @return
     */
    @Override
    public R2 controUser(Integer status, Integer id) {
        R2 r = new R2();
        if(status == 0){
            Integer r1 = userMapper.forbidden(id);
            if(r1>0){
                r.setCode(Constants.SUCCESS);
                r.setMsg("成功");
            }else {
                r.setCode(500);
                r.setMsg("失败");
            }
        }else{
            Integer r2 = userMapper.start(id);
            if(r2>0){
                System.out.println();
                r.setCode(Constants.SUCCESS);
                r.setMsg("成功");
            }else {
                r.setCode(500);
                r.setMsg("失败");
            }
        }
        return r;
    }

    /**
     * 查询待审核用户列表
     * @return
     */
    @Override
    public R2 queryAudit(Map map) {
        Set<String> set = map.keySet();
        int a = 0;
        for (String key:set) {
            if(key.equals("endDate")){
                a=1;
                break;
            }
        }
        Integer currentPage = 0;
        Integer limit = 5;
        for(String key : set){
            System.out.println();
            if(key.equals("limit")){
                currentPage = (Integer)map.get("currentPage");
                limit = (Integer)map.get("limit");
                currentPage = (currentPage-1)*limit;
            }
        }
        System.out.println();
        map.put("currentPage",currentPage);
        map.put("limit",limit);
        if(a==1){
            if(map.get("beginDate")!=null&&map.get("beginDate")!=""){
                System.out.println();
                String beginDate = (String)map.get("beginDate");
                String endDate = (String)map.get("endDate");
                beginDate = beginDate +" 00:00:00";
                endDate = endDate+" 00:00:00";
                map.put("beginDate",beginDate);
                map.put("endDate",endDate);
            }
        }
        List<User> list = userMapper.queryAudit(map);
        Integer total = userMapper.queryAuditTotal(map);
        R2 r = new R2();
        if(list != null){
            r.setCode(Constants.SUCCESS);
            r.setMsg("success");
            r.setData(list);
            r.setData2(total);
        }else {
            r.setCode(500);
            r.setMsg("false");
        }
        return r;
    }

    //单个审核用户
    @Override
    public R2 auditOne(Integer id) {
        Audit audit = userMapper.auditOne(id);
        R2 r = new R2();
        if(audit!=null){
            r.setData(audit);
            r.setCode(Constants.SUCCESS);
            r.setMsg("success");
        }else{
            r.setCode(500);
            r.setMsg("false");
        }
        return r;
    }

    //通过审核或不通过
    @Override
    public R2 audit(Integer id, Integer status) {
        R2 r = new R2();
        //1代表通过,2代表不通过
        if (status == 1) {
            Integer result = userMapper.agree(id);
            if (result > 0) {
                r.setCode(Constants.SUCCESS);
                r.setMsg("success");
            } else {
                r.setCode(500);
                r.setMsg("false");
            }
        }
        if (status == 2) {
            Integer result = userMapper.refuse(id);
            if (result > 0) {
                r.setCode(0);
                r.setMsg("success");
            } else {
                r.setCode(500);
                r.setMsg("false");
            }
        }
        return r;
    }

    //批量审核
    @Override
    public R2 auditSome(String[] ids) {
        R2 r = new R2();
        Integer result = userMapper.auditSome(ids);
        if(result>0){
            r.setCode(Constants.SUCCESS);
            r.setMsg("success");
            System.out.println();
        }else{
            r.setCode(500);
            r.setMsg("false");
        }
        return r;
    }

    @Override
    public R2 deleteAudit(Integer id) {
        Integer result = userMapper.deleteAudit(id);
        R2 r = new R2();
        if(result>0){
            r.setCode(Constants.SUCCESS);
            r.setMsg("successs");
        }else {
            r.setCode(500);
            r.setMsg("false");
        }
        return r;
    }

    @Override
    public R2 queryUserById(Integer id) {
        User user = userMapper.queryUserById(id);
        R2 r = new R2();
        if(user!=null){
            r.setData(user);
            r.setCode(0);
            r.setMsg("success");
        }else {
            r.setMsg("false");
            r.setCode(500);
        }
        return r;
    }

}
